package com.xudm.cdg.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xudm.cdg.annotation.CurrentVisitor;
import com.xudm.cdg.config.MinioConfig;
import com.xudm.cdg.entity.ResponseResult;
import com.xudm.cdg.entity.system.FileGenerationRecord;
import com.xudm.cdg.mapper.system.FileGenerationRecordMapper;
import com.xudm.cdg.service.CosmicDocumentgenerateService;
import com.xudm.cdg.service.MinioService;
import com.xudm.cdg.service.system.IFileGenerationRecordService;
import io.minio.ObjectWriteResponse;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/cosmic")
public class CosmicDocementController {
    @Resource
    private CosmicDocumentgenerateService documentgenerateService;

    @Resource
    private MinioService minioService;

    @Resource
    private MinioConfig minioConfig;

    @Resource
    private FileGenerationRecordMapper fileGenerationRecordMapper;

    @PostMapping("/generate")
    public void generate(@RequestParam("file") MultipartFile file) throws IOException {
        // 生成cosmic文档
        documentgenerateService.readExcelAndWriterToLocal(file);
    }

    @PostMapping("/uploadFile")
    public ResponseResult<?> uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        //调用minio接口
        String originalFilename = file.getOriginalFilename();
        String fileExt = originalFilename != null ? originalFilename.substring(originalFilename.lastIndexOf(".") + 1) : null;
        String filePath = StrUtil.format("{}/{}.{}", "generate", IdUtil.simpleUUID(), fileExt);
        String fullPath = StrUtil.format("{}/{}/{}", minioConfig.getEndpoint(), minioConfig.getBucketName(), filePath);
        ObjectWriteResponse objectWriteResponse = minioService.uploadFile(file, filePath);

        if (ObjectUtil.isNotNull(objectWriteResponse) && StrUtil.isNotBlank(objectWriteResponse.object())) {
            Map<String, Object> resMap = new HashMap<>();
            resMap.put("fileExt", fileExt);
            resMap.put("fileSize", file.getSize());
            resMap.put("filePath", fullPath);
            resMap.put("message", "上传成功");
            resMap.put("code", 1000);
            return ResponseResult.success(resMap);
        } else {
            return ResponseResult.failed("上传失败，请联系管理员检查存储服务！");
        }
    }

    @RequestMapping("/getFileGenerationRecordList")
    public ResponseResult<?> getFileGenerationRecordList(@CurrentVisitor String visitorId) {
        // 查询文件生成记录
        LambdaQueryWrapper<FileGenerationRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FileGenerationRecord::getCreatedBy, visitorId);
        Page<FileGenerationRecord> page = new Page<>(1, 10);
        return ResponseResult.success(fileGenerationRecordMapper.selectPage(page, queryWrapper));
    }
}
